package game;

import misc.Vector3i;
import player.ComputerPlayer;
import player.HumanPlayer;
import board.Board;
import board.BoardUtil;
import character.Character;
import item.Club;
import item.Lance;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.BulletAppState.ThreadingType;
import com.jme3.font.BitmapText;
import com.jme3.math.Vector3f;
import com.jme3.system.AppSettings;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main extends SimpleApplication {
    protected ResolutionController ticker;
    public static BitmapText ticksText;

    public static void main(String[] args) {
        Main app = new Main();
        app.preStart();
        app.start();
    }
    
    protected void preStart() {
        Logger.getLogger("").setLevel(Level.INFO);
	AppSettings cfg = new AppSettings(true);
	cfg.setFrameRate(60); // set to less than or equal screen refresh rate
	//cfg.setVSync(true);   // prevents page tearing - destroying my framerate?
	//cfg.setFrequency(60); // set to screen refresh rate
	cfg.setResolution(800, 600);
	cfg.setFullscreen(false);
	//cfg.setSamples(2);    // anti-aliasing
	cfg.setTitle("My Game"); // branding: window name
	//try {
	  // Branding: window icon
	  //cfg.setIcons(new BufferedImage[]{ImageIO.read(new File("assets/Interface/icon.gif"))});
	//} catch (IOException ex) {
	  //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "Icon missing.", ex);
	//}
	// branding: load splashscreen from assets
	cfg.setSettingsDialogImage("Interface/MySplashscreen.png");
	setShowSettings(false);
	setSettings(cfg);
    }

    @Override
    public void simpleInitApp() {
	initDataStore();
	initBullet();
	initPlayers();
	initBoard();
        initGui();

	addCharacter(0, new Vector3i(3,0,8));
        addCharacter(0, new Vector3i(6,0,8));
	((ComputerPlayer)TurnController.getPlayer(1)).setCharacter(addCharacter(1, new Vector3i(3,0,3)));
        
        Club club = new Club();
        club.setBoardLocation(new Vector3i(6,0,4));
        club.setLocalTranslation(BoardUtil.boardToWorld(new Vector3i(6,0,4)));
        DataStore.board.addItem(club);
        
        Lance lance = new Lance();
        lance.setBoardLocation(new Vector3i(5,0,7));
        lance.setLocalTranslation(BoardUtil.boardToWorld(new Vector3i(5,0,7)));
        DataStore.board.addItem(lance);
        
        StateController.init();
    }

    protected void initDataStore() {
	DataStore.stateManager = stateManager;
        DataStore.inputManager = inputManager;
	DataStore.assetManager = assetManager;
	DataStore.rootNode = rootNode;
    }

    protected void initPlayers() {
	flyCam.setDragToRotate(true);
	flyCam.setMoveSpeed(10f);
	cam.setLocation(new Vector3f(4f, 12f, 15f));
	cam.lookAt(new Vector3f(6.5f, 0f, 7.5f), Vector3f.UNIT_Y);

	TurnController.addPlayer(new HumanPlayer("Human", cam));
	TurnController.addPlayer(new ComputerPlayer("Compy"));
    }

    protected void initBullet() {
	BulletAppState bulletAppState = new BulletAppState();
	bulletAppState.setThreadingType(ThreadingType.PARALLEL);
	getStateManager().attach(bulletAppState);
        bulletAppState.getPhysicsSpace().enableDebug(assetManager);
	DataStore.bulletAppState = bulletAppState;
    }

    protected void initBoard() {
	Board board = new Board(10, 10);
        rootNode.attachChild(board);
        DataStore.bulletAppState.getPhysicsSpace().addAll(board);
        DataStore.board = board;
    }
    
    protected void initGui() {
        guiNode.detachAllChildren();
        guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
        ticksText = new BitmapText(guiFont, false);
        ticksText.setSize(guiFont.getCharSet().getRenderedSize());
        ticksText.setText("Hello World");
        ticksText.setLocalTranslation(0, 600, 0);
        guiNode.attachChild(ticksText);
    }

    protected Character addCharacter(int playerId, Vector3i location) {
	Character character = new Character(TurnController.getPlayer(playerId));
        DataStore.bulletAppState.getPhysicsSpace().add(character);
        character.setBoardLocation(location);
	character.getCharacterControl().warp(BoardUtil.boardToWorld(location));
	DataStore.board.addCharacter(character);
	return character;
    }

    @Override
    public void simpleUpdate(float tpf) {}
}
